<?php
/*========================================= JooSam =========================================/
/																							/
/	Extention:		JooSam																	/
/	Created by:		ScriptPlanet.eu															/
/	Site:			http://www.scriptplanet.eu												/
/	Email:			support@scriptplanet.eu													/
/	Support:		http://www.scriptplanet.eu/support										/
/	Copyright:		2011 (c) ScriptPlanet.eu												/
/	License:		CC BY-NC-ND 3.0 ~ http://creativecommons.org/licenses/by-nc-nd/3.0/		/
/	File Version:	0001																	/
/																							/
/========================================== JooSam ========================================*/
// Joomla Security
defined('_JEXEC') or die;
JHTML::_('behavior.mootools');
jimport('joomla.application.component.helper');
if(JComponentHelper::isEnabled('com_joosam', true)):
	
	$colorTitle = $params->get('title');
	$colorData = $params->get('data');
	$colorTimer = $params->get('timer');
	$scriptMarge = $params->get('marge');
	$scriptRefres = $params->get('refres');
	$thxON = $params->get('thonoff');
	$thxType = $params->get('thtype');
	$thxMid = $params->get('thmarid');
	$defMtxt = $params->get('defTxt');
	$thxTXT = $params->get('thxTxt');
	$thxUrl = JRoute::_('index.php?option=com_joosam&view=modules&layout=live&format=raw');
	echo '
	<style type="text/css">
		.jsam_mod_live .jsam_table, #jsamLive{ width:100%; }
		.jsam_mod_live .jsam_row .jsam_td_artist, .jsam_mod_live .jsam_row .jsam_td_title{ width:50px; margin:0; padding:0 4px 0 0; text-align:left; color:#'.$colorTitle.'; }
		.jsam_mod_live .jsam_row .jsam_td_artist_data, .jsam_mod_live .jsam_row .jsam_td_title_data{ margin:0; padding:0; text-align:left; color:#'.$colorData.'; }
		.jsam_mod_live .jsam_row .jsam_th_timer{ margin:0; padding:5px 0 0 0; font-weight:bold; color:#'.$colorTimer.'; }
	</style>'; ?>
<div id="output" class="jsam_mod_live">
<input type="hidden" id="jsamLiveSid" value="0"/>
<input type="hidden" id="jsamTimeH" value="0"/>
<input type="hidden" id="jsamTimeM" value="0"/>
<input type="hidden" id="jsamTimeS" value="0"/>
<input type="hidden" id="jsamTimeR" value="no"/>
</div>

<script>
	
	// ID check

	function jsamLchkObject(){
		var req;
		if(window.XMLHttpRequest){
			req = new XMLHttpRequest();
		}
		else if(window.ActiveXObject){
			req = new ActiveXObject("Microsoft.XMLHTTP");
		}
		else{
			alert("Your browser is to old to show this page.");
		}
		return req;
	}
	var jsamLck = jsamLchkObject();
	
	function jsamLchkSend(url){
		jsamLck.open('post', url ,true);
		jsamLck.onreadystatechange = jsamLchkHandleResponse;
		jsamLck.send(null);
	}

	function jsamLchkHandleResponse(){
		if(jsamLck.readyState == 4 && jsamLck.status == 200){
			var target = document.getElementById('jsamLiveSid');
			var response = eval("(" + jsamLck.responseText + ")");

			if(response.state == 'playing' || response.state == 'offline'){
				var realid = response.songid;
				var docid = target.value;

				if(docid != realid){
					var reset = document.getElementById('jsamTimeR');
					reset.value = 'yes';
				}
			}
			else{
				var reset = document.getElementById('jsamTimeR');
				reset.value = 'yes';
			}
		}
	}

	// Output
	function jsamLiveObject(){
		var req;
		if(window.XMLHttpRequest){
			req = new XMLHttpRequest();
		}
		else if(window.ActiveXObject){
			req = new ActiveXObject("Microsoft.XMLHTTP");
		}
		else{
			alert("Your browser is to old to show this page.");
		}
		return req;
	}
	var jsamLiv = jsamLiveObject();

	function jsamLiveSend(url){
		jsamLiv.open('post', url ,true);
		jsamLiv.onreadystatechange = jsamLiveHandleResponse;
		jsamLiv.send(null);
	}

	function jsamLiveHandleResponse(){
		if(jsamLiv.readyState == 4 && jsamLiv.status == 200){
			var target = document.getElementById('output');
			var response = eval("(" + jsamLiv.responseText + ")");
			if(response.state != 'playing'){
				var offline =	'<h1>Offline</h1>'+
								'<input type="hidden" id="jsamTimeR" value="offline"/>'+
								'<input type="hidden" id="jsamLiveSid" value="'+response.songid+'"/>';

				target.innerHTML = offline;
			}
			else{
				var imgdata = '';
				if(response.imgtype == 'passed' || response.imgtype == 'lastfmerr'){
					imgdata = response.imgdata[0];
				}
				else{
					imgdata = response.imgdata;
				}

				// Build Data's
				var picture;
				
				if(response.imgtype == 'passed'){
					picture = '<img src="'+imgdata+'" style="height:'+response.imgheight+';width:'+response.imgwidth+';border:none;margin:0;padding:0;" />';
				}
				else{
					picture = '<p class="jsamImgErr" style="height:'+response.imgheight+';width:'+response.imgwidth+';border:none;margin:0;padding:0;">'+imgdata+'</p>';
				}

				var hidden = '<input type="hidden" id="jsamLiveSid" value="'+response.songid+'"/>';
				var count;
				var hours = response.hour_left;
				var mins = response.min_left;
				var secs = response.sec_left;
				var marges = "<?php echo $scriptMarge; ?>";
				hours = parseFloat(hours);
				mins = parseFloat(mins);
				secs = parseFloat(secs);
				marges = parseFloat(marges);

				secs = secs + marges;
				
				// 3min or more by sec
				if(secs >= 180){ secs = secs - 180; count = 3; }
				// 2<>3 min by sec
				else if(secs >= 120){ secs = secs - 120; count = 2; }
				// 1<>2 min by sec
				else if(secs >= 60){ secs = secs - 60; count = 1; }
				// 0<>1 min by sec
				else if(secs >= 0){ count = 0; }
				// 0<>-60 min by sec
				else if(secs >= -60){ secs = secs + 60; count = -1; }
				// -60<>-120 min by sec
				else if(secs >= -120){ secs = secs + 120; count = -2; }
				// -120<>-180 min by sec
				else if(secs >= -180){ secs = secs + 180; count = -3; }
				else{ secs = secs + 180; count = -3; }

				mins = mins + count;

				if(mins >= 60){
					hours = hours+1;
					mins = mins - 60;
				}
				else if(mins <= 0){
					hours = hours - 1;
					mins = mins + 60;
				}

				if(hours < 0){
					hours = 0;
					mins = 0;
					secs = 59;
				}
				
				var data = '	<div class="jsam_data">'+
									'<table border="0" class="jsam_table">'+
										'<tr class="jsam_row">'+
											'<td class="jsam_td_artist">'+response.lang_artist+':</td>'+
											'<td class="jsam_td_artist_data">'+response.artist+'</td>'+
										'</tr>'+
										'<tr class="jsam_row">'+
											'<td class="jsam_td_title">'+response.lang_title+':</td>'+
											'<td class="jsam_td_title_data">'+response.title+'</td>'+
										'</tr>'+
										'<tr class="jsam_row">'+
											'<th colspan="2" class="jsam_th_timer">'+
												'<input type="hidden" id="jsamTimeH" value="'+hours+'"/>'+
												'<input type="hidden" id="jsamTimeM" value="'+mins+'"/>'+
												'<input type="hidden" id="jsamTimeS" value="'+secs+'"/>'+
												'<input type="hidden" id="jsamTimeR" value="no"/>'+
												'<div id="countje"></div>'+
											'</th>'+
										'</tr>'+
									'</table>'+
								'</div>';
								
				target.innerHTML = picture + hidden + data;

				var thxOn = "<?php echo $thxON; ?>";
				var thxType = "<?php echo $thxType; ?>";
				var thxMid = "<?php echo $thxMid; ?>";
				var thxText = "<?php echo $thxTXT; ?>";
				var thxDmTxt = "<?php echo $defMtxt; ?>";
				var reqID = response.reqID;
				var reqErr = response.reqErr;
				var reqName = response.reqName;
				var reqMsg = response.reqMsg;

				if(thxOn == '1' && thxType == 'divb' && reqErr == 'passed'){
					var build = document.createElement('div');
					build.id = 'jsam_div_thanks';
					if( document.body.firstChild){ document.body.insertBefore(build, document.body.firstChild); }
					else{ document.body.appendChild(build); }
					var userWidth = window.innerWidth;
					var divPosX = userWidth / 2 - 150;
					var posLeft = 'left:'+divPosX+'px;';

					var reqTarget = document.getElementById("jsam_div_thanks");
					var cssStyle = 'text-align:center; width:300px; background:#fff; color:#000; border:1px dotted #000; position:absolute; z-index:10000;' + posLeft ;
					if(reqTarget){
						reqTarget.setAttribute('style', cssStyle);
						var reqOut = '<h4 style="margin:0;padding:0;">'+reqName+'</h4><p style="margin:0;padding:2px;">'+thxText+'</p><p style="margin:0;padding:2px;">'+reqMsg+'</p>';
						reqTarget.innerHTML = reqOut;
					}
				}
				else if(thxOn == '1' && thxType == 'marq' && reqErr == 'passed'){
					if((thxMid != '') && (thxMid != 'undefined')){
						var reqTarget = document.getElementById(thxMid);
						if(reqTarget){
							var reqOut = '<span style="padding:0 20px 0 0;">'+reqName+'</span><span style="padding:0 20px 0 0;">'+thxText+'</span><span>Message by '+reqName+': '+reqMsg+'</span>';
							reqTarget.innerHTML = reqOut;
						}
					}
				}
				else{
					if(thxType == 'divb'){
						var reqTarget = document.getElementById("jsam_div_thanks");
						if(reqTarget){
							document.body.removeChild(reqTarget);
						}
					}
					else if(thxType == 'marq'){
						var reqTarget = document.getElementById(thxMid);
						if(reqTarget){
							reqTarget.innerHTML = thxDmTxt;
						}
					}
				}
			}
		}
	}

	window.onload = setcountnow();

	
	function setcountnow(){

		var reset = document.getElementById('jsamTimeR');

		if(reset.value == 'offline'){
			jsamLiveSend("<?php echo JRoute::_('index.php?option=com_joosam&view=data&layout=live&format=raw&check=full'); ?>");
			setTimeout("setcountnow()", 10000);
		}
		else{
			var hour = document.getElementById('jsamTimeH');
			var min = document.getElementById('jsamTimeM');
			var sec = document.getElementById('jsamTimeS');
			var hours = hour.value;
			var mins = min.value;
			var secs = sec.value;
			hours = parseFloat(hours);
			mins = parseFloat(mins);
			secs = parseFloat(secs);

			if(mins <= 0 && hours >= 1){ mins = 60; hours = hours - 1; }
			if(secs <= 0 && mins >= 1){ secs = 60; mins = mins - 1; }

			if((hours <= 0 && mins <= 0 && secs <= 0) || (reset.value == 'yes')){
				jsamLiveSend("<?php echo JRoute::_('index.php?option=com_joosam&view=data&layout=live&format=raw&check=full'); ?>");
				setTimeout("setcountnow()", 1000);
			}
			else{
				secs = secs -1;
				hour.value = hours;
				min.value = mins;
				sec.value = secs;
				if(secs < 10){ secs = '0'+secs; }

				if(hours > 0){
					if(mins < 10){ mins = '0'+mins; }
					document.getElementById("countje").innerHTML = hours + ':' + mins + ':' + secs;
				}
				else{
					document.getElementById("countje").innerHTML = mins + ':' + secs;
				}
				jsamLchkSend("<?php echo JRoute::_('index.php?option=com_joosam&view=data&layout=live&format=raw&check=idonly'); ?>");
				setTimeout("setcountnow()",1000);
			}
		}
	}
				
</script> <?php
else: ?>
	<div id="jsamLive">
		<div style="background:white; color:red; font-weight:bold; border-radius:10px; margin:1px; text-align:center; padding:0;">
			Component joosam is not installed.
		</div>
	</div><?php
endif;
?>